<template>
  <div class="xtx-goods-page" v-if="detail.categories">
    <div class="container">
      <!-- 面包屑 -->
      <Bread separator="/">
        <BreadItem to="/">首页</BreadItem>
        <BreadItem to="/">{{ detail.categories[1].name }}</BreadItem>
        <BreadItem to="/">{{ detail.categories[0].name }}</BreadItem>
        <BreadItem to="/">{{ detail.name }}</BreadItem>
      </Bread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 新增结构 -->
        <!-- 图片预览区 -->
        <div class="media">
          <leftImages :images="detail.mainPictures"></leftImages>
          <GoodsSales></GoodsSales>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodsName></GoodsName>
          <GoodsSku :goods="detail" @change="getSku"></GoodsSku>
          <NumBox :min="1" :max="detail.inventory" v-model="buyNum"></NumBox>
          <Button
            size="middle"
            type="primary"
            style="margin-top: 20px"
            @click="addCart"
            >加入购物车</Button
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <img
              v-imgLazy="url"
              v-for="url in detail.details.pictures"
              :key="url"
              alt=""
            />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { getGoodsDetailApi } from '@/api/goods'
import { ref, provide } from 'vue'
import { useRoute } from 'vue-router'
import leftImages from './components/goods-image.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSales from './components/goods-sales.vue'
import msg from '@/components/Message/index.js'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    leftImages,
    GoodsName,
    GoodsSales
  },
  setup () {
    const detail = ref({})
    provide('detail', detail)
    const route = useRoute()
    const getGoodsDetail = async () => {
      const { result } = await getGoodsDetailApi(route.params.id)
      // console.log(result)
      detail.value = result
    }
    getGoodsDetail()
    const buyNum = ref(1)
    const currentSel = ref(null)
    const getSku = (sku) => {
      // console.log('sku', sku)
      if (sku.skuId) {
        detail.value.price = sku.price
        detail.value.oldPrice = sku.oldPrice
        detail.value.inventory = sku.inventory
        // 存储sku信息
        currentSel.value = sku
      } else {
        currentSel.value = null
      }
    }
    const store = useStore()
    const addCart = async () => {
      if (!currentSel.value) {
        return msg({ text: '请选择商品规格' })
      }
      if (currentSel.value.inventory === 0) {
        return msg({ text: '商品库存不足' })
      }
      const currentGood = {
        id: detail.value.id,
        name: detail.value.name,
        picture: detail.value.mainPictures[0],
        skuId: currentSel.value.skuId, // sku属性Id(唯一)
        price: currentSel.value.oldPrice, // 价格
        nowPrice: currentSel.value.price,
        attrsText: currentSel.value.specsText,
        stock: currentSel.value.inventory, // 库存
        selected: true, // 是否在购物车中选中
        isEffective: true, // 是否是有效商品
        count: buyNum.value // 购买数量
      }
      try {
        const res = await store.dispatch('cart/addCartAction', currentGood)
        msg({ text: res, type: 'success' })
      } catch (error) {
        console.log(error)
        msg({ text: '加入购物车失败', type: 'error' })
      }
    }
    return { detail, getSku, buyNum, addCart }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  // 新增样式
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
  img {
    width: 100%;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
